﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using SelfService.Model.Models;


namespace SelfService.ViewModel.SysClientVMs
{
    public partial class SysClientListVM : BasePagedListVM<SysClientView, SysClientSearcher>
    {

        protected override IEnumerable<IGridColumn<SysClientView>> InitGridHeader()
        {
            return new List<GridColumn<SysClientView>>{
                this.MakeGridHeader(x => x.Name),
                this.MakeGridHeader(x => x.Code),
                this.MakeGridHeader(x => x.Address),
                this.MakeGridHeader(x => x.Ip),
                this.MakeGridHeader(x => x.MacAddress),
                this.MakeGridHeader(x => x.Version),
                this.MakeGridHeader(x => x.ModuleList),
                this.MakeGridHeader(x => x.DeviceList),
                this.MakeGridHeader(x => x.CardList),
                this.MakeGridHeader(x => x.PayList),
                this.MakeGridHeader(x => x.IsUpdate),
                this.MakeGridHeader(x => x.LastTime),
                this.MakeGridHeader(x => x.LoginTime),
                this.MakeGridHeader(x => x.ThemeName),
                this.MakeGridHeaderAction(width: 200)
            };
        }

        public override IOrderedQueryable<SysClientView> GetSearchQuery()
        {
            var query = DC.Set<SysClient>()
                .CheckContain(Searcher.Name, x=>x.Name)
                .CheckContain(Searcher.Code, x=>x.Code)
                .CheckContain(Searcher.Address, x=>x.Address)
                .CheckContain(Searcher.Ip, x=>x.Ip)
                .CheckContain(Searcher.MacAddress, x=>x.MacAddress)
                .CheckContain(Searcher.Version, x=>x.Version)
                .CheckWhere(Searcher.SelectedModulesIDs,x=>DC.Set<ClientModules>().Where(y=>Searcher.SelectedModulesIDs.Contains(y.ModuleId)).Select(z=>z.ClientId).Contains(x.ID))
                .CheckWhere(Searcher.SelectedDevicesIDs,x=>DC.Set<ClientDevice>().Where(y=>Searcher.SelectedDevicesIDs.Contains(y.DeviceId)).Select(z=>z.ClientId).Contains(x.ID))
                .CheckEqual(Searcher.IsUpdate, x=>x.IsUpdate)
                .CheckBetween(Searcher.LastTime?.GetStartTime(), Searcher.LastTime?.GetEndTime(), x => x.LastTime, includeMax: false)
                .CheckBetween(Searcher.LoginTime?.GetStartTime(), Searcher.LoginTime?.GetEndTime(), x => x.LoginTime, includeMax: false)
                .Select(x => new SysClientView
                {
				    ID = x.ID,
                    Name = x.Name,
                    Code = x.Code,
                    Address = x.Address,
                    Ip = x.Ip,
                    MacAddress = x.MacAddress,
                    Version = x.Version,
                    ModuleList = x.Modules.Select(y=>y.Module.Name).ToSepratedString(null,","), 
                    DeviceList = x.Devices.Select(y=>y.Device.Name).ToSepratedString(null,","),
                    CardList = x.CardTypes.Select(y => y.CardType.Name).ToSepratedString(null, ","),
                    PayList = x.PayTypes.Select(y => y.PayType.Name).ToSepratedString(null, ","),
                    IsUpdate = x.IsUpdate,
                    LastTime = x.LastTime,
                    LoginTime = x.LoginTime,
                    ThemeName = x.Theme.Name,
                    NationOperationId = x.User.NationOperationId,
                    NationOperationName = x.User.NationOperationName
                })
                .OrderBy(x => x.ID);
            return query;
        }

    }

    public class SysClientView : SysClient{
        [Display(Name = "模块")]
        public String ModuleList { get; set; }
        [Display(Name = "硬件")]
        public String DeviceList { get; set; }
        [Display(Name = "卡")]
        public String CardList { get; set; }
        [Display(Name = "支付")]
        public String PayList { get; set; }
        [Display(Name = "主题")]
        public String ThemeName { get; set; }
        
        [Display(Name = "医保操作员ID")]
        public string NationOperationId { get; set; }
        
        [Display(Name = "医保操作员名称")]
        public string NationOperationName { get; set; }
    }
}
